package ru.digitanoise.dnaccountapi.api.internal.service;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
import org.springframework.web.multipart.MultipartFile;
import ru.digitanoise.dnaccountapi.api.internal.model.dto.RequestSendCodeDTO;
import ru.digitanoise.dnaccountapi.api.internal.model.dto.TokenActiveRequestDTO;
import ru.digitanoise.dnaccountapi.api.internal.model.dto.TokenRequestDTO;
import ru.digitanoise.dnutils.SystemEntity;

@Service
@RequiredArgsConstructor
public class InternalApiService {

    RestClient restClient = RestClient.create();
    @Value("${uri.token-host}")
    private String tokenHost;

    @Value("${uri.image-host}")
    private String imageHost;

    @Value(("${uri.notification-host}"))
    private String notificationHost;

    public Long validActiveCode(String activeCode) {
        return restClient
                .post()
                .uri(tokenHost)
                .contentType(MediaType.APPLICATION_JSON)
                .body(new TokenActiveRequestDTO(activeCode, SystemEntity.ACTIVE_ACCOUNT))
                .retrieve()
                .toEntity(Long.class)
                .getBody();
    }

    public String saveImage(MultipartFile file) {
        return restClient
                .post()
                .uri(imageHost + "/save")
                .contentType(MediaType.MULTIPART_FORM_DATA)
                .body(file)
                .retrieve()
                .toEntity(String.class)
                .getBody();
    }

    public String generateCode(Long id) {
        return restClient
                .post()
                .uri(tokenHost)
                .body(new TokenRequestDTO(id, SystemEntity.ACTIVE_ACCOUNT))
                .retrieve()
                .toEntity(String.class)
                .getBody();
    }

    public void sendEmailActive(String email, String code) {
        restClient
                .post()
                .uri(notificationHost)
                .body(new RequestSendCodeDTO(email, SystemEntity.ACTIVE_ACCOUNT, code));
    }
}
